<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" >
<head>
    <meta charset="UTF-8">
    <title>管理员列表</title>
    <link rel="stylesheet" type="text/css" th:href="@{/manager/easyui/themes/default/easyui.css}">
    <link rel="stylesheet" type="text/css" th:href="@{/manager/easyui/themes/icon.css}">
    <script type="text/javascript" th:src="@{/manager/easyui/jquery.min.js}"></script>
    <script type="text/javascript" th:src="@{/manager/easyui/jquery.easyui.min.js}"></script>
	<script type="text/javascript">
	    //工具栏
	    var toolbar = [{
	        text: '新增',
	        iconCls: 'icon-add',
	        handler: function () {
	            $('#dd').dialog('open');
                $('#ff').attr('action','/manager/managerUser/addUser')



            }
	    }, '-',{
	        text: '修改',
	        iconCls: 'icon-edit',
	        handler: function () {
	            $('#dd').dialog('open');
                $('#ff').attr('action','/manager/managerUser/modifyUser')
                var row =$('#userlist').datagrid('getSelected');
                $('#id').textbox('setValue',row.id);
                $('#username').textbox('setValue',row.username);
                $('#password').textbox('setValue',row.password);

                for(var j=0;j<row.role.length;j++){
                    var node = $('#ah').tree('find',row.role[j].id);
                    $('#ah').tree('check',node.target)
                }



	        }
	    }];
	   $(function(){
	       $('#userlist').datagrid({  
			title:'管理员列表',  
			iconCls:'icon-save',   
			url:'/manager/managerUser/findAll',
			columns:[
			    [
				{field:'id',title:'管理员ID',width:100,align:'center'},
				{field:'username',title:'管理员名称',width:100,align:'center'},
				{field:'password',title:'管理员密码',width:100,align:'center'},
				{field:'roles',title:'拥有的角色',width:200,align:'center',formatter:format}
			]
            ],
			pagination:true,
			toolbar: toolbar,
               singleSelect:true,
               pageSize: 5,
               pageList: [5,10,20]
		});
		 //初始化查询栏
		 $("#submit_search").linkbutton({ iconCls: 'icon-search', plain: true })
            .click(function () {
                $('#userlist').datagrid({ queryParams: {
                    username:$('#myUserName').val()

                    } });   //点击搜索
            });
          
          //初始化修改窗口  
         $('#dd').dialog({
			    title: 'My Dialog',
			    width: 700,
			    height: 500,
			    closed: true,
			    cache: false,
			    href: '',
			    modal: true,
			    iconCls: 'icon-save',
                buttons: [{
                    text:'提交',
                    iconCls:'icon-ok',
                    handler:function(){

                        var ids="";
                        var nodes = $('#ah').tree('getChecked');

                        for(var i=0;i<nodes.length;i++){
                            ids+=nodes[i].id
                            if(i!=nodes.length-1){
                                ids+=",";
                            }
                        }

                        $('#myPId').attr('value',ids);
                        $('#ff').submit()

                        alert('ok');



                    }
                },{
                    text:'关闭',
                    handler:function(){
                        $('#dd').dialog('close');
                        alert('cancel');;
                    }
                }]
			});
			
		   //tree选项框初始化
           $("#ah").tree({
               url:'/manager/managerUser/findAllRole',
			   method:'post',
			   animate:true,
			   checkbox:true,
               formatter:function (node) {
                   return node.name
               }


           });
            
	   });
	   
	    //列格式化方法
	   function format(val,row){
	        var sel="<select name='search_type' style='width:100%'>";

           if(row.role!=null){
           for(var i=0;i<row.role.length;i++){
               sel+="<option>"+row.role[i].name+"</option>";
           }

           }
                sel+= "</select>";
			return sel;
			}
	</script>
</head>
<body>
   <form name="searchform" method="post" action="" id ="searchform">
    <td width="70" height="30"><strong>管理员查询：</strong></td>
    <td height="30">
        <input type="text" name="keyword" id="myUserName" size=20 placeholder="管理员名称"/>
        <a id="submit_search">搜索</a>
    </td>
  </form>
  <table id="userlist"></table>
    <div id="dd">
           <div align="center" style="margin-top: 30px">
            <form id="ff" method="post">
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" id="id" name="id" style="width:50%" data-options="label:'管理员ID:'" readonly="readonly"/>
            </div>
             <div style="margin-bottom:20px">
                <input class="easyui-textbox" type="password" id="password" name="password" style="width:50%" data-options="label:'管理员密码:',required:true">
            </div>
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="username" id="username" style="width:50%" data-options="label:'管理员名称:',required:true">
            </div>
            <div style="margin-bottom:20px">
                  <table style="width:48%">
                    <td valign="top" style="width:50px">
                                                              角色:
                    </td>
                    <td><ul id="ah"></ul></td>
                  </table>
                <input type="hidden" name="myPermissionId" id="myPId">
            </div>
        </form>
        </div>
    </div>
</body>
</html>